#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
分页管理器
用于处理样本管理和系统日志的分页功能
"""

import logging
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass

logger = logging.getLogger(__name__)

@dataclass
class PaginationInfo:
    """分页信息"""
    current_page: int = 1
    page_size: int = 20
    total_items: int = 0
    total_pages: int = 0
    has_previous: bool = False
    has_next: bool = False
    start_index: int = 0
    end_index: int = 0

class PaginationManager:
    """分页管理器"""
    
    def __init__(self, page_size: int = 20):
        self.page_size = page_size
        self.current_page = 1
        self.total_items = 0
        self.total_pages = 0
        self._all_data = []
        self._filtered_data = []
        self._current_data = []
    
    def set_data(self, data: List[Any]):
        """设置数据"""
        self._all_data = data
        self._filtered_data = data
        self.total_items = len(data)
        self._calculate_pages()
        self._update_current_data()
    
    def set_filtered_data(self, data: List[Any]):
        """设置过滤后的数据"""
        self._filtered_data = data
        self.total_items = len(data)
        self.current_page = 1  # 重置到第一页
        self._calculate_pages()
        self._update_current_data()
    
    def _calculate_pages(self):
        """计算分页信息"""
        self.total_pages = (self.total_items + self.page_size - 1) // self.page_size
        if self.total_pages == 0:
            self.total_pages = 1
    
    def _update_current_data(self):
        """更新当前页数据"""
        start_index = (self.current_page - 1) * self.page_size
        end_index = min(start_index + self.page_size, self.total_items)
        
        if start_index < self.total_items:
            self._current_data = self._filtered_data[start_index:end_index]
        else:
            self._current_data = []
    
    def get_current_data(self) -> List[Any]:
        """获取当前页数据"""
        return self._current_data
    
    def get_pagination_info(self) -> PaginationInfo:
        """获取分页信息"""
        start_index = (self.current_page - 1) * self.page_size
        end_index = min(start_index + self.page_size, self.total_items)
        
        return PaginationInfo(
            current_page=self.current_page,
            page_size=self.page_size,
            total_items=self.total_items,
            total_pages=self.total_pages,
            has_previous=self.current_page > 1,
            has_next=self.current_page < self.total_pages,
            start_index=start_index + 1 if self.total_items > 0 else 0,
            end_index=end_index
        )
    
    def go_to_page(self, page: int) -> bool:
        """跳转到指定页"""
        if 1 <= page <= self.total_pages:
            self.current_page = page
            self._update_current_data()
            return True
        return False
    
    def next_page(self) -> bool:
        """下一页"""
        return self.go_to_page(self.current_page + 1)
    
    def previous_page(self) -> bool:
        """上一页"""
        return self.go_to_page(self.current_page - 1)
    
    def first_page(self) -> bool:
        """第一页"""
        return self.go_to_page(1)
    
    def last_page(self) -> bool:
        """最后一页"""
        return self.go_to_page(self.total_pages)
    
    def set_page_size(self, page_size: int):
        """设置每页大小"""
        if page_size > 0:
            self.page_size = page_size
            self.current_page = 1  # 重置到第一页
            self._calculate_pages()
            self._update_current_data()
    
    def get_page_info_text(self) -> str:
        """获取页面信息文本"""
        info = self.get_pagination_info()
        if info.total_items == 0:
            return "暂无数据"
        return f"第 {info.current_page} 页，共 {info.total_pages} 页 | 显示 {info.start_index}-{info.end_index} 条，共 {info.total_items} 条"
    
    def reset(self):
        """重置分页器"""
        self.current_page = 1
        self.total_items = 0
        self.total_pages = 0
        self._all_data = []
        self._filtered_data = []
        self._current_data = []
